﻿#include"stdafx.h"
#ifdef LK_LEAKCHECKING_
namespace LK
{
	LeakCheckingObject::~LeakCheckingObject()
	{
		if (this == &Default())
		{
			if (map().size() == 0)return;
			std::map<void*, LeakCheckingObject*>::iterator iter = map().begin();
			while (iter != map().end())
			{
				void *p = iter->first;
				std::cout << "文件:" << iter->second->fileName << std::dec << " 行:" << iter->second->line << " 大小:" << iter->second->size << " 地址:" << p << std::endl;
				std::cout << "  内容(hex):";
				for (size_t i = 0; i < 256 && i < iter->second->size; ++i)std::cout << std::hex << (int)((unsigned char*)p)[i];
				std::cout << "\n  内容(char):  ";
				for (size_t i = 0; i < 256 && i < iter->second->size; ++i)std::cout << ((unsigned char*)p)[i];
				std::cout << "\n  内容(wchar):  ";
				for (size_t i = 0; i < 256 && i < iter->second->size / sizeof(wchar_t); ++i)std::wcout << ((wchar_t*)p)[i];
				std::cout << std::endl;
				++iter;
			}
			std::cout << "一共检测到" << std::dec << map().size() << "处泄漏";
			std::cin.sync();
			std::cin.get();
		}
	}

	void LeakCheckingObject::Remove(void*p)
	{
		if (p == &map() || p == &mutex())
			return;
		if (p)
		{
			mutex().Lock();
			std::map<void*, LeakCheckingObject*>::iterator pp = map().find(p);
			if (pp != map().end())
			{
				pp->second->~LeakCheckingObject();
				free(pp->second);
			}
			map().erase(p);
			mutex().UnLock();
		}
	}

}
#ifdef _DEBUG
#endif
#endif
